package com.cy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cy.bean.GameList;
import com.cy.bean.UserList;
import com.cy.util.ConnectionFactory;

public class AboutMeDaoImpl implements AboutMeDao {

	@Override
	public UserList findAllById(int userid) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		try {
			String sql = "select * from user where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, userid);
			rs = psmt.executeQuery();
			while (rs.next()) {
				UserList user = new UserList();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getInt("sex"));
				user.setAge(rs.getInt("age"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setEmail(rs.getString("email"));
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return null;
	}

	@Override
	public int findTotal(int userid) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		int total = 0;
		try {
			String sql = "select count(*) as total from sell_info where user_id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, userid);
			rs = psmt.executeQuery();
			while (rs.next()) {
				total = rs.getInt("total");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return total;
	}

	@Override
	public List<GameList> findAllByPage(int parseInt, int userid) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		try {
			String sql = "select sell_date,game_id from sell_info where user_id=? limit ?,8";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, userid);
			psmt.setInt(2, parseInt);
			rs = psmt.executeQuery();
			List<GameList> mygames = new ArrayList<>();
			while (rs.next()) {
				mygames.add(selectMyGame(rs.getInt("game_id"), rs.getString("sell_date")));
			}
			return mygames;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return null;
	}

	private GameList selectMyGame(int gameid, String adddate) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		try {
			String sql = "select * from games where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, gameid);
			rs = psmt.executeQuery();
			while (rs.next()) {
				GameList game = new GameList();
				game.setGamename(rs.getString("game_name"));
				game.setPublisher(rs.getString("publisher"));
				game.setAdddate(rs.getString("add_date"));
				game.setIntroduce(rs.getString("introduce"));
				game.setType(rs.getString("type"));
				game.setBuydate(adddate);
				return game;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return null;
	}

}
